package com.besall.allbase.view.activity.chipstoollevel4.protocol.InfoParams;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;

public class ConnectionKey extends DialogFragment {

    public EditText seed_1;
    public EditText seed_2;
    public EditText seed_3;
    public EditText seed_4;
    public EditText value_1;
    public EditText value_2;
    public EditText value_3;
    public EditText value_4;

    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_connectionkey, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        seed_1 = (EditText) view.findViewById(R.id.seed1);
        seed_2 = (EditText) view.findViewById(R.id.seed2);
        seed_3 = (EditText) view.findViewById(R.id.seed3);
        seed_4 = (EditText) view.findViewById(R.id.seed4);
        value_1 = (EditText) view.findViewById(R.id.value1);
        value_2 = (EditText) view.findViewById(R.id.value2);
        value_3 = (EditText) view.findViewById(R.id.value3);
        value_4 = (EditText) view.findViewById(R.id.value4);

        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    public boolean saveConfig(){
        String s1 = seed_1.getText().toString();
        if (TextUtils.isEmpty(s1)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_SEED1_VALUE, s1);

        String s2 = seed_2.getText().toString();
        if (TextUtils.isEmpty(s2)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_SEED2_VALUE, s2);

        String s3 = seed_3.getText().toString();
        if (TextUtils.isEmpty(s3)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_SEED3_VALUE, s3);

        String s4 = seed_4.getText().toString();
        if (TextUtils.isEmpty(s4)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_SEED4_VALUE, s4);

        String v1 = value_1.getText().toString();
        if (TextUtils.isEmpty(v1)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_VALUE1_VALUE, v1);

        String v2 = value_2.getText().toString();
        if (TextUtils.isEmpty(v2)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_VALUE2_VALUE, v2);

        String v3 = value_3.getText().toString();
        if (TextUtils.isEmpty(v3)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_VALUE3_VALUE, v3);

        String v4 = value_4.getText().toString();
        if (TextUtils.isEmpty(v4)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 255", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_VALUE4_VALUE, v4);

        return true;
    }

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
